﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using anaii.EasyNews.Data;

namespace anaii.EasyNews
{
    public partial class Settings : ModuleSettingsBase 
    {
        public override void LoadSettings()
        {
            try
            {
                if (!IsPostBack)
                {
                    BindData();
                }
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        public override void UpdateSettings()
        {
            try
            {
                ModuleController controller = new ModuleController();
                int i = 0;
                foreach (KeyValuePair<string, Locale> val in Localization.GetLocales(this.PortalId))
                {
                    TextBox txtNewsTemplate = repTemplates.Items[i++].FindControl("txtNewsTemplate") as TextBox;
                    if(txtNewsTemplate != null)
                        controller.UpdateTabModuleSetting(TabModuleId, "NewsTemplate" + val.Key, txtNewsTemplate.Text);
                    controller.UpdateTabModuleSetting(TabModuleId, "NewsItemSelectedNewsType", ddlNewsType.SelectedValue);
                    controller.UpdateTabModuleSetting(TabModuleId, "NewsItemSelectedNewsStatus", rblStatus.SelectedValue);
                    controller.UpdateTabModuleSetting(TabModuleId, "NewsItemShowDefaultItem", cbShowDefaultItem.Checked.ToString());
                    controller.UpdateTabModuleSetting(TabModuleId, "NewsItemQueryStringKey", txtNewsIDQueryString.Text.Trim());
                }
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        private void BindData()
        {
            EasyNewsDBDataContext db = new EasyNewsDBDataContext();
            ddlNewsType.Items.Clear();
            ddlNewsType.Items.Add(new ListItem("All", "0"));
            foreach (EasyNewsType ent in db.EasyNewsTypes)
            {
                ddlNewsType.Items.Add(new ListItem(ent.Name, ent.TypeID.ToString()));
            }
            if (!string.IsNullOrEmpty((string)TabModuleSettings["NewsItemSelectedNewsType"]))
            {
                ddlNewsType.SelectedValue = (string)TabModuleSettings["NewsItemSelectedNewsType"];
            }
            if (!string.IsNullOrEmpty((string)TabModuleSettings["NewsItemQueryStringKey"]))
            {
                txtNewsIDQueryString.Text = (string)TabModuleSettings["NewsItemQueryStringKey"];
            }
            else
            {
                txtNewsIDQueryString.Text = "NewsID";
            }

            rblStatus.Items.Clear();
            rblStatus.Items.Add(new ListItem("Published", "1"));
            rblStatus.Items.Add(new ListItem("Archive", "2"));
            rblStatus.SelectedIndex = 0;
            if (!string.IsNullOrEmpty((string)TabModuleSettings["NewsItemSelectedNewsStatus"]))
            {
                rblStatus.SelectedValue = (string)TabModuleSettings["NewsItemSelectedNewsStatus"];
            }
            if (!string.IsNullOrEmpty((string)TabModuleSettings["NewsItemShowDefaultItem"]))
            {
                cbShowDefaultItem.Checked = bool.Parse((string)TabModuleSettings["NewsItemShowDefaultItem"]);
            }

            repTemplates.DataSource = Localization.GetLocales(this.PortalId);
            repTemplates.DataBind();
        }

        protected void repTemplates_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem != null)
            {
                KeyValuePair<string, Locale> valueData = (KeyValuePair<string, Locale>)e.Item.DataItem;

                TextBox txtNewsTemplate = e.Item.FindControl("txtNewsTemplate") as TextBox;
                if (txtNewsTemplate != null)
                {
                    if ((string)TabModuleSettings["NewsTemplate" + valueData.Key] != string.Empty)
                    {
                        txtNewsTemplate.Text = (string)TabModuleSettings["NewsTemplate" + valueData.Key];
                    }
                }
            }
        }
    }
}